'''
Created on 09.06.2013

@author: Michael
'''

from libavg import avg
import user
import field

'''
Represents user stats as output. Can be attached to root node.
Shows Player avatar, name, upgrade stats and visualizes if a player is dead.
'''
class PlayerStats(avg.DivNode):

    def __init__(self, user, parent = None, fieldGraphics=field.FieldGraphics(), **kwargs):
        if user is None:
            return
        self.__fieldGraphics = fieldGraphics
        self.__user = user
        super(PlayerStats, self).__init__(**kwargs)
        self.registerInstance(self, parent)
        
        # set child nodes
        #self.__back = avg.RectNode(size = self.size, color="CCCCCC", parent=self)
        self.__nameNode = avg.WordsNode(font="calibri", alignment="center", color="000000", letterspacing=2, fontsize=25, text=self.__user.getName(), size = (self.size[0], 25), parent = self, pos=(self.size[0]/2, 5))
        self.__charNode = avg.ImageNode(href=self.__user.getCharImage(None), parent=self, pos=(0,30))
        imgSize = self.__charNode.getMediaSize()
        factor = ((self.size[0])/imgSize[0])*0.8
        self.__charNode.size = (factor*imgSize[0],factor*imgSize[1])
        self.__charNode.x = (self.size[0]-factor*imgSize[0])/2
        
        stat_bar_top = 30 + factor*imgSize[1] + 10
        stat_bar_size = 25
        stat_bar_gap = (self.size[0] - 3 * stat_bar_size)/4 # there are 4 gaps (3 icons)
        self.__bombUpNode =  avg.ImageNode(href=self.__fieldGraphics.bomb, parent=self, size=(stat_bar_size, stat_bar_size), pos=(stat_bar_gap, stat_bar_top))
        self.__speedUpNode =  avg.ImageNode(href=self.__fieldGraphics.speed, parent=self, size=(stat_bar_size, stat_bar_size), pos=(2*stat_bar_gap+stat_bar_size, stat_bar_top))
        self.__strengthUpNode =  avg.ImageNode(href=self.__fieldGraphics.strength, parent=self, size=(stat_bar_size, stat_bar_size), pos=(3*stat_bar_gap+2*stat_bar_size, stat_bar_top))
        
        stat_bar_top += stat_bar_size
        self.__bombUpTextNode =  avg.WordsNode(font="calibri", alignment="center", color="000000", letterspacing=2, fontsize=20, text='x' + str(self.__user.getBombStat()), parent=self, 
                                               size=(stat_bar_size, stat_bar_size), pos=(stat_bar_gap+stat_bar_size/2, stat_bar_top))
        self.__speedUpTextNode =  avg.WordsNode(font="calibri", alignment="center", color="000000", letterspacing=2, fontsize=20, text='x' + str(self.__user.getSpeedStat()),parent=self, 
                                                size=(stat_bar_size, stat_bar_size), pos=(2*stat_bar_gap+stat_bar_size+stat_bar_size/2, stat_bar_top))
        self.__strengthUpTextNode =  avg.WordsNode(font="calibri", alignment="center", color="000000", letterspacing=2, fontsize=20, text='x' + str(self.__user.getStrengthStat()),parent=self, 
                                                   size=(stat_bar_size, stat_bar_size), pos=(3*stat_bar_gap+2*stat_bar_size+stat_bar_size/2, stat_bar_top))
        
        self.height = stat_bar_top + 25
        
    # takes a new user and updates field accordingly
    def updateUser(self, user):
        self.__user = user
        self.__bombUpTextNode.text = 'x' + str(self.__user.getBombStat())
        self.__speedUpTextNode.text = 'x' + str(self.__user.getSpeedStat())
        self.__strengthUpTextNode.text = 'x' + str(self.__user.getStrengthStat())
        
        if user.isDead():
            blur = avg.BlurFXNode()
            blur.radius = 5
            self.__charNode.setEffect(blur)
            
            self.__charNode.opacity = 0.5
            self.__bombUpNode.opacity = 0.5
            self.__speedUpNode.opacity = 0.5
            self.__strengthUpNode.opacity = 0.5    